﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using HNet;
using ProtoBuf;
using Debug = UnityEngine.Debug;

public class NetTest2 : MonoBehaviour
{
    private Stopwatch sw;

    private void Awake()
    {
        sw = new Stopwatch();
        NetCoroutinue.Instance.Processing.RegisterBytesMeth("test", test, this);
    }

    private void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Send();
        }
    }

    public void Send()
    {
        sw.Reset();
        sw.Start();

        Data d = new Data
        {
            str = "ZI FU CHUAN",
            value = 1.22f,
            Name = "Haha"
        };
        NetCoroutinue.Instance.SendBytesMeth(new int[] { Message.AllClients }, "test", ProtocolBuffer.Serialize<Data>(d));
    }

    //TODO 继续完成测试2
    public void test(byte[] param)
    {
        sw.Stop();
        Data d = ProtocolBuffer.DeSerialize<Data>(param);

        Debug.Log(d.Name);
        Debug.Log(d.value);
        Debug.Log(d.str);

        Debug.Log(sw.ElapsedMilliseconds);

        GetComponent<Rigidbody>().AddForce(Vector3.up * 100);
    }

    [ProtoContract]
    public struct Data
    {
        [ProtoMember(1)]
        public string Name;

        [ProtoMember(2)]
        public float value;

        [ProtoMember(3)]
        public string str;
    }
}